<template>
  <div class="app-container">
    <div class="top_div">
      <p class="top-title">注册红包设置</p>
      <div>
        <!-- <k-button class="top-btn" value="玩法介绍" @click.native="ruleGuideDialog" /> -->
        <el-switch
          :disabled="!authorityHandle(67)"
          v-model="registerRedpocketSwitch"
          active-color="#3b425d"
          inactive-color="#E9EBF2"
          active-text="开启"
          inactive-text="关闭"
          :active-value="1"
          :inactive-value="2"
          @change="registerStateChange()"
        />
      </div>
    </div>
    <div class="Related_container">
      <div class="set_box">
        <div v-for="(item,index) in registerRedSettingItemList" :key="item.id" class="Recharge_Data">
          <div class="Recharge_Data_box">
            <span>红包档位:</span>
            <el-input v-model.trim="item.minAmount" :disabled="registerRedpocketSwitch==1?true:false" />
            <i style="margin: 0 2px;"> — </i>
            <el-input v-model.trim="item.maxAmount" style="margin-right:20px" :disabled="registerRedpocketSwitch==1?true:false" />
            <span>档位概率:</span>
            <el-input v-model.trim="item.probability" :disabled="registerRedpocketSwitch==1?true:false" /> %
          </div>
          <div v-show="registerRedpocketSwitch==1?false:true" class="operation">
            <i v-show="index != 0" class="el-icon-remove" @click="Delete(item.id,index)" />
            <i v-show="index == registerRedSettingItemList.length - 1" class="el-icon-circle-plus" @click="newly_added()" />
          </div>
        </div>
      </div>
    </div>
    <div class="footer">
      <button v-if="authorityHandle(67)" class="btnStyle" @click="save">保存</button>
      <span v-if="!authorityHandle(69)">总计：{{ ammount }}</span>
    </div>
    <!-- 玩法介绍 弹窗 -->
    <k-dialog
      :show-hide.sync="ruleGuide.showHide"
      :modal-append-to-body="false"
      :append-to-body="true"
      title="玩法介绍"
      class-name="ruleGuide-dialog"
      :show-foot="false"
      :two-btn="authorityHandle(67)"
      @click="guideSave"
    >
      <div class="dialog-ruleGuide" style="padding:30px">
        <el-form :model="ruleGuideContent" label-width="auto">
          <el-form-item label="内容介绍：" prop="content">
            <quill-editor
              ref="myQuillEditor"
              v-model.trim="ruleGuideContent.content"
              :options="editorOption"
              class="editer"
            />
          </el-form-item>
        </el-form>
      </div>
    </k-dialog>
  </div>
</template>

<script>
import { getRegisterTotal, setRegisterData, getRegisterData, getRegisterStatus, setRegisterStatus, getDescribe, setDescribe } from '@/api/red/data'

export default {
  components: {},
  data() {
    return {
      ammount: 0,
      ruleGuide: {
        showHide: false
      },
      registerRedpocketSwitch: 1,
      registerRedSettingItemList: [{
        id: undefined,
        minAmount: undefined,
        maxAmount: undefined,
        probability: undefined
      }],
      ruleGuideContent: {
        content: ''
      },
      editorOption: { //  富文本编辑器配置
        placeholder: '请输入内容',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            ['blockquote', 'code-block'], // 引用，代码块
            [{ 'header': 1 }, { 'header': 2 }], // 标题，键值对的形式；1、2表示字体大小
            [{ 'list': 'ordered' }, { 'list': 'bullet' }], // 列表
            [{ 'script': 'sub' }, { 'script': 'super' }], // 上下标
            [{ 'indent': '-1' }, { 'indent': '+1' }], // 缩进
            [{ 'direction': 'rtl' }], // 文本方向
            [{ 'size': ['small', false, 'large', 'huge'] }], // 字体大小
            [{ 'header': [1, 2, 3, 4, 5, 6, false] }], // 几级标题
            [{ 'color': [] }, { 'background': [] }], // 字体颜色，字体背景颜色
            [{ 'font': [] }], // 字体
            [{ 'align': [] }], // 对齐方式
            ['clean'], // 清除字体样式
            ['image'] // 上传图片、上传视频
          ]
        }
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getTotal()
    this.getSettingItem()
    this.getRedPocketState()
    this.getDescribe()
  },
  mounted() {},
  methods: {
    getTotal() {
      getRegisterTotal().then(res => {
        this.ammount = res.data
      })
    },
    ruleGuideDialog() {
      this.ruleGuide.showHide = true
    },
    // 保存玩法管理
    guideSave() {
      const contents = this.ruleGuideContent.content
      setDescribe({ 'key': 'envelopeRegister', 'data': contents }).then(res => {
        this.ruleGuide.showHide = false
      })
    },
    // 加载玩法管理
    getDescribe() {
      getDescribe({ key: 'envelopeRegister' }).then(res => {
        this.ruleGuideContent.content = res.data.msg
      })
    },
    // 获取支付方式开关
    getRedPocketState() {
      getRegisterStatus().then(res => {
        this.registerRedpocketSwitch = res.data
        this.$log(res.data, this.registerRedpocketSwitch)
      })
    },
    // 支付方式开关
    registerStateChange() {
      setRegisterStatus({ 'status': this.registerRedpocketSwitch }).then(res => {
        this.$log(res)
      })
    },
    getSettingItem() {
      getRegisterData().then(res => {
        this.registerRedSettingItemList = res.data
      })
    },
    // 保存
    save() {
      const probSum = this.registerRedSettingItemList.map(item => {
        return item.probability
      }).reduce(function(a, b) {
        return Number(a) + Number(b)
      })
      this.$log(probSum)

      if (100 - Number(probSum).toFixed(2) !== 0) {
        this.$message.error('档位概率和要等于100,还需要 ' + (100 - probSum).toFixed(2))
        return
      }
      setRegisterData(this.registerRedSettingItemList).then(res => {
        this.$message.success('保存成功')
      })
    },
    // 设置新增
    newly_added() {
      this.registerRedSettingItemList.push({
        id: undefined,
        minAmount: undefined,
        maxAmount: undefined,
        probability: undefined
      })
    },
    // 设置删除
    Delete(value, indexItem) {
      if (!value) {
        this.registerRedSettingItemList.splice(indexItem, 1)
        return
      }
      var index = this.registerRedSettingItemList.findIndex(function(item) {
        return item.id === value
      })
      if (index !== -1) {
        this.registerRedSettingItemList.splice(index, 1)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
$mainColor:#3b425d;// 主色调
$disabledColor: #909399; //禁用色
$hoverColor:#1D143C;// 鼠标停留主色调浅色
  .editer {
    width: 500px;
    border-radius: 8px;
    float: left;
    box-sizing: border-box;
    color: #606266;
    ::v-deep .ql-container {
      height: 270px;
      border-radius: 0px 0px 8px 8px;
    }
    ::v-deep .ql-toolbar.ql-snow {
      border-radius: 8px 8px 0px 0px;
    }
}
.app-container{

  .top_div{
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #F5F5F5 ;
    .top-btn{
      margin: 0px 30px;
      font-size: 14px;
      color: #ffffff;
    }
  }
  .top-title {
      height: 25px;
      line-height: 25px;
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 500;
      color: #14162E;
      margin-left: 10px;
      padding-left: 10px;
      border-left: 4px solid #3b425d;
    }
  .Related_container {
    width: 900px;
    height: 500px;
    padding: 60px 50px;
    border-radius: 10px;
    border: 1px dashed #ccc;
    margin: 80px auto 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    .el-input__inner:focus {
      border-color: #3b425d
    }
  .set_box {
    width: 700px;
    padding: 0 50px;
    box-sizing: border-box;
    .Recharge_Data {
      width: 100%;
      height: 40px;
      margin-top: 10px;
      display: flex;
      align-items: center;
      .Recharge_Data_box {
        width: 100%;
        height: 40px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        .el-input {
          width: 150px;
          height: 35px;
          input {
            width: 150px;
            height: 35px;
          }
        }
        span {
          font-size: 14px;
          width: 100px;
        }
      }
      .operation {
        width: 12%;
        display: flex;
        justify-content: space-between;
        .el-icon{
            &-circle-plus, &-remove{
              font-size: 22px;
              color:#3b425d;
              margin-left: 10px;
          }
        }
      }
     }
    }
  }
  .footer {
    text-align: center;
    margin-top: 30px;
    :nth-child(1){
      margin-right: 20px;
    }
  }
}

</style>
